export default abstract class ChannelTools extends cc.Component {
    private backdrop: cc.Node;

    private toast: cc.Node;
    private toast_bg: cc.Node;
    private toast_content: cc.Label;

    private dialog: cc.Node;
    private dialog_title: cc.Label;
    private dialog_content: cc.Label;
    private dialog_sure: cc.Node;
    private dialog_cancel: cc.Node;
    private dialog_sure_label: cc.Label;
    private dialog_cancel_label: cc.Label;
    private dialog_ad_icon: cc.Node;
    private dialog_sure_cb: Function;
    private dialog_cancel_cb: Function;

    private loading:cc.Node;

    onLoad() {
        this.initWindow();
        cc.game.addPersistRootNode(this.node);
        this.backdrop = this.node.getChildByName("backdrop");
        this.toast = this.node.getChildByName("toast");
        this.toast_bg = this.toast.getChildByName("bg");
        this.toast_content = this.toast.getChildByName("content").getComponent(cc.Label);

        this.Init();

        this.dialog = this.node.getChildByName("dialog");
        this.dialog_title = this.dialog.getChildByName("title").getComponent(cc.Label);
        this.dialog_content = this.dialog.getChildByName("content").getComponent(cc.Label);
        this.dialog_sure = this.dialog.getChildByName("btn_sure");
        this.dialog_cancel = this.dialog.getChildByName("btn_cancel");
        this.dialog_sure_label = this.dialog_sure.getChildByName("label").getComponent(cc.Label);
        this.dialog_cancel_label = this.dialog_cancel.getChildByName("label").getComponent(cc.Label);
        this.dialog_ad_icon = this.dialog_sure.getChildByName("ad_icon");
        this.dialog_sure.on(cc.Node.EventType.TOUCH_END, this.onDialogSureClick, this);
        this.dialog_cancel.on(cc.Node.EventType.TOUCH_END, this.onDialogCancelClick, this);

        this.loading = this.node.getChildByName("loading");
    }

    private initWindow(){
        let windowSize = cc.winSize;
        this.node.width = windowSize.width;
        this.node.height = windowSize.height;
        this.node.x = windowSize.width * 0.5;
        this.node.y = windowSize.height * 0.5;
    }

    protected abstract Init();

    /**
     * Toast提示
     * @param content 提示内容，单行显示，建议不要过长
     */
    public abstract ShowToast(content: string);
    /**
     * Toast提示(私有)
     * @param content 提示内容，单行显示，建议不要过长
     */
    protected showToast(content: string) {
        this.hideToast();
        this.toast_content.string = content;
        this.toast.active = true;
        this.toast_bg.width = this.toast_content.node.width * 0.4 + 30;
        this.scheduleOnce(this.toastAnim, 1.2);
    }
    /**
     * Toast动画
     */
    private toastAnim() {
        this.toast.runAction(cc.sequence(cc.fadeTo(0.5, 0), cc.callFunc(this.hideToast, this)));
    }
    /**
     * 隐藏Toast提示
     */
    private hideToast() {
        this.unschedule(this.toastAnim);
        this.toast.stopAllActions();
        this.toast.active = false;
        this.toast.opacity = 255;
    }

    /**
     * 显示提示框
     * @param data 提示框数据
     * @param ->title string 提框框标题(选填)，默认为“提 示”
     * @param ->content string 提框框标题(必填)，提示的内容
     * @param ->sure Function 点击确认的回调(选填)
     * @param ->cancel Function 点击取消的回调(选填)
     * @param ->sure_label 确认按键上的文字(选填)，默认为“确 定”
     * @param ->cancel_label string 取消按键上的文字(选填)，默认为“取 消”
     * @param ->ad_icon boolean 是否有广告图标(选填)，默认没有
     */
    public abstract ShowDialog(data: any);
    /**
     * 显示提示框(私有)
     * @param data 提示框数据
     * @param ->title string 提框框标题(选填)，默认为“提 示”
     * @param ->content string 提框框标题(必填)，提示的内容
     * @param ->sure Function 点击确认的回调(选填)
     * @param ->cancel Function 点击取消的回调(选填)
     * @param ->sure_label 确认按键上的文字(选填)，默认为“确 定”
     * @param ->cancel_label string 取消按键上的文字(选填)，默认为“取 消”
     * @param ->ad_icon boolean 是否有广告图标(选填)，默认没有
     */
    protected showDialog(data: any) {
        this.hideDialog();
        this.backdrop.active = true;
        //显示标题
        if (data.title) {
            this.dialog_title.string = data.title;
        } else {
            this.dialog_title.string = "提 示";
        }

        //显示内容
        if (data.content) {
            this.dialog_content.string = data.content;
        } else {
            this.dialog_content.string = "err";
        }

        //修改确定按键文字
        if (data.sure_label) {
            this.dialog_sure_label.string = data.sure_label;
        } else {
            this.dialog_sure_label.string = "确 定";
        }

        //修改取消按键文字
        if (data.cancel_label) {
            this.dialog_cancel_label.string = data.cancel_label;
        } else {
            this.dialog_cancel_label.string = "取 消";
        }

        /**
         * 显示广告图标
         */
        if (data.ad_icon) {
            this.dialog_sure_label.node.x = 28;
            this.dialog_ad_icon.active = true;
        } else {
            this.dialog_sure_label.node.x = 0;
            this.dialog_ad_icon.active = false;
        }

        if (!(data.sure || data.cancel)) {
            this.dialog_sure.x = 0;
            this.dialog_cancel.active = false;
        } else {
            this.dialog_sure.x = -160;
            this.dialog_cancel.x = 160;
            this.dialog_cancel.active = true;
        }

        this.dialog_sure_cb = data.sure;
        this.dialog_cancel_cb = data.cancel;
        this.dialog.active = true;
        this.dialog.runAction(cc.scaleTo(0.2, 1));
        //
    }

    private onDialogSureClick() {
        if (this.dialog_sure_cb) {
            this.dialog_sure_cb();
        }
        this.dialogMinAnim();
    }

    private onDialogCancelClick() {
        if (this.dialog_cancel_cb) {
            this.dialog_cancel_cb();
        }
        this.dialogMinAnim();
    }

    /**
     * 提示框缩小动画
     */
    private dialogMinAnim() {
        this.dialog.runAction(cc.sequence(cc.scaleTo(0.2, 0), cc.callFunc(this.hideDialog, this)));
    }

    /**
     * 隐藏提示框
     */
    private hideDialog() {
        this.unschedule(this.dialogMinAnim);
        this.dialog.stopAllActions();
        this.dialog.active = false;
        this.dialog.scale = 0;
        if(!this.loading.active)this.backdrop.active = false;
    }

    /**
     * 显示加载界面
     */
    public ShowLoading(){
        this.loading.active = true;
        this.backdrop.active = true;
    }

    /**
     * 隐藏加载界面
     */
    public HideLoading(){
        this.loading.active = false;
        if(!this.dialog.active)this.backdrop.active = false;
    }
}
